package zmee.pmas.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * @Author cb
 * @DateTime 2022/12/7 10:58
 * @Description 资质后审设置、编辑插件
 */
public class AptitudeConfigFormPlugin extends AbstractFormPlugin {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        // 父级页面的单据对象
        DynamicObject dataEntity = this.getView().getParentView().getModel().getDataEntity(true);
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("parentid", "=", pkValue);
        // 查询 评语设置
        DynamicObject bidopenConfig = BusinessDataServiceHelper.loadSingle("src_bidopen_config",
                "id,entryentity,entryentity.scorer", new QFilter[]{qFilter});
        if (null != bidopenConfig) {
            List<DynamicObject> ids = new ArrayList<>();
            // 获取单据体信息
            DynamicObjectCollection entryentity = bidopenConfig.getDynamicObjectCollection("entryentity");
            if (CollectionUtils.isNotEmpty(entryentity)) {
                // 获取第一行
                DynamicObject dynamicObject = entryentity.get(0);
                // 获取 评委 字段的值
                DynamicObjectCollection scorer = dynamicObject.getDynamicObjectCollection("scorer");
                for (DynamicObject user : scorer) {
                    DynamicObject fbasedataid = user.getDynamicObject("fbasedataid");
                    // 将评为用户放入list
                    ids.add(fbasedataid);
                }

                QFilter qFilter1 = new QFilter("parentid", "=", pkValue);
                // 获取 资质后审设置
                DynamicObject queryOne = QueryServiceHelper.queryOne("src_aptitudeconfig2", "id", new QFilter[]{qFilter1});
                if (null != queryOne) {
                    // 查询完整的 资质后审设置 对象
                    DynamicObject srcAptitudeconfig2 = BusinessDataServiceHelper.loadSingle(queryOne.get("id"), "src_aptitudeconfig2");
                    // 获取单据体信息
                    DynamicObjectCollection entrys = srcAptitudeconfig2.getDynamicObjectCollection("entryentity");
                    for (int i = 0; i < entrys.size(); i++) {
                        DynamicObjectCollection scorers = entrys.get(i).getDynamicObjectCollection("scorer");
                        for (DynamicObject id : ids) {
                            DynamicObject dy = new DynamicObject(scorers.getDynamicObjectType());
                            dy.set("fbasedataId", id);
                            scorers.add(dy);
                        }
                        this.getModel().setValue("scorer", scorers, i);
                    }
                }
            }
        }
    }
}